import { get, post, put, del } from '@/utils/request';
import { 
  User, 
  CreateUserRequest, 
  UpdateUserRequest, 
  PaginatedResponse 
} from './types';

// 用户管理相关 API
export const usersApi = {
  // 获取用户列表
  getUsers: (params?: {
    page?: number;
    pageSize?: number;
    username?: string;
    status?: string;
  }): Promise<PaginatedResponse<User>> =>
    get('/users', params),

  // 获取单个用户
  getUser: (id: number): Promise<User> =>
    get(`/users/${id}`),

  // 创建用户
  createUser: (data: CreateUserRequest): Promise<User> =>
    post('/users', data),

  // 更新用户
  updateUser: (id: number, data: UpdateUserRequest): Promise<User> =>
    put(`/users/${id}`, data),

  // 删除用户
  deleteUser: (id: number): Promise<void> =>
    del(`/users/${id}`),

  // 批量删除用户
  batchDelete: (ids: number[]): Promise<void> =>
    del('/users', { data: { ids } }),

  // 修改密码
  changePassword: (id: number, password: string): Promise<void> =>
    put(`/users/${id}/password`, { password }),

  // 获取用户统计信息
  getStats: (): Promise<any> =>
    get('/users/stats/overview'),

  // 获取用户创建选项
  getCreationOptions: (): Promise<any> =>
    get('/users/options/creation'),

  // 分配角色
  setRoles: (id: number, roleIds: number[]) => put(`/users/${id}/roles`, { roleIds }),

  // 获取用户所有菜单/按钮权限（树形结构）
  getMenus: (id: number) => get(`/users/${id}/menus`),
}; 